<?php
//用户控制器
class Controller_Admin_UserV2 extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'user';
        $this->_list_pagesize = 20;
        parent::__construct($req, $res);

        $this->_list_where = array(
            array('status', '!=', 99),
        );

        $this->_sort_extra_columns = array(
            array('changed', DB::expr('curdate()')),
        );
    }

	//默认列表显示页
    public function action_index ($page = 1)
    {
        $this->action_list($page);
    }

    // 删除用户
    public function action_remove ($uid)
    {
        $user = User::getUserFromUid($uid);
        $user->remove();

        $this->to_url('list');
    }

    public function action_list($page = 1){
        $filter = array();
        $query  = $this->request->query();


        $filter['userName'] = Arr::get($query, 'name');
        $filter['source'] = Arr::get($query, 'source');
        $filter['utype']  = Arr::get($query, 'utype');
        $filter['email']  = Arr::get($query, 'email');
        $filter['mobile']  = Arr::get($query, 'mobile');
        if(!empty($filter['userName'])){
            $this->_list_where[] = array('userName', 'like', '%'.$filter['userName'].'%');
        }

        if(!empty($filter['source'])){
            $this->_list_where[] = array('source', '=', $filter['source']);
        }

        if(!empty($filter['utype'])){
            $this->_list_where[] = array('utype', '=', $filter['utype']);
        }

        if(!empty($filter['email'])){
            $this->_list_where[] = array('email', '=', $filter['email']);
        }

        $this->template->set('form_include_head_file', $this->_form->include_edit_file());
        $this->template->set('filter', $filter);

        parent::action_list($page);
    }

	//
    public function action_messages($uid)
    {
        $limit = 100;
        $offset = 0;

        $message = new App_Message($uid);
        $messages = $message->getList($offset, $limit);
        $this->template->set('title', '我的消息');
        $this->template->set('messages', $messages);
        $this->template->set_filename('smarty:admin/userv2/messages');
    }

	//修改用户信息
	//$nid int 用户id
    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
        }


        parent::action_update($nid);

    }

	//用户详细信息
	//$nid int 用户id
    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 
        $this->_form_data = $data;
        $this->_form_data['userIcon'] = explode('|', $data['userIcon']);
        parent::action_detail($nid);
    }

    public function detail_filter_config_data (& $arr_config)
    {
        //
    }

    private function rebuild_post_data (& $post)
    {
        //
    }

	//重构数据信息
    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            $ret = App_Account::userOrderCount($rs['uid']);
//            $rs['coins'] = $ret['coin'];
            $data = Util_User::getUserInfo($rs['uid']);

            $rs['moneyOrder'] = '￥'.number_format($ret['moneyOrder']/ 100, 2);
            $rs['countsOrder'] = $ret['countsOrder'];
            $rs['source'] = strtolower($rs['source']);
            $rs['mobile'] = $data['userPhone'];
            switch ($rs['source'])
            {
                case 'wechat':
                    $rs['source'] = '微信';
                    break;
                case 'qq':
                    $rs['source'] = 'QQ';
                    break;
                case 'sina':
                    $rs['source'] = '新浪';
                    break;
                case 'mobile':
                    $rs['source'] = '手机';
                    break;
                case 'web':
                    $rs['source'] = '网站';
                    break;

                default: 
                    $rs['source'] = '';
                    break;
            }

            switch ($rs['utype'])
            {
                case 'GUIDER':
                    $rs['utype'] = '导游';
                    break;

                case 'TOURIST':
                    $rs['utype'] = '游客';
                    break;
            }

            switch ($rs['userGender'])
            {
                case 'f':
                    $rs['userGender'] = '女';
                    break;
                case 'm':
                    $rs['userGender'] = '男';
                    break;

                default: 
                    $rs['userGender'] = '--';
                    break;
            }
        }
    }

	//菜单列表
    public function actions_by_list_row (& $row, & $menus)
    {
        unset($menus);

        $menus[] = '<a href="/admin/order/user/'.$row['uid'].'">订单</a>';
        $menus[] = '<a target="_blank" href="/admin/chat/chat_list/'.$row['uid'].'">聊天记录</a>';
        $menus[] = '<a href="/admin/userv2/remove/'.$row['uid'].'">删除</a>';
    }
}
